/*
GANESHA - Reasoning on critical infrastructures affected by emergency situations
Copyright (c) 2010-2011, Information Eng. Research Unit - Univ. of Alcala
http://www.cc.uah.es/ie
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2.1 of the License, or (at your option)
any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License along
with this library; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

package es.uah.cc.ie.ganesha.gis;

import com.esri.arcgis.geodatabase.FeatureClass;
import com.esri.arcgis.geodatabase.IFeatureClass;
import com.esri.arcgis.geodatabase.IFeatureCursor;
import com.esri.arcgis.geodatabase.SpatialFilter;
import com.esri.arcgis.geodatabase.esriSpatialRelEnum;
import com.esri.arcgis.geometry.Point;
import com.esri.arcgis.geometry.Polygon;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Class that represents a layer on a GIS.
 * @author ieru
 */
public class GISLayer {

	private IFeatureClass _featureClass;

	/**
	 * Constructor
	 * @param featureClass Layer
	 */
	public GISLayer(IFeatureClass featureClass)
	{
		_featureClass = featureClass;
	}

	/**
	 * Performs a query by distance from the given point
	 * @param latitude Latitude.
	 * @param longitude Longitude.
	 * @param distance Distance in degrees (0.1 deg lon = 8.5km aprox, 0.1 deg lat = 11km aprox)
	 * @return Returns all the elements in the layer that are in the specified distance or closer from the specified point.
	 */
	public IFeatureCursor distanceQuery(double latitude, double longitude, double distance)
	{
		try {
			SpatialFilter filter = new SpatialFilter();
			filter.setSpatialRel(esriSpatialRelEnum.esriSpatialRelContains);
			Polygon area = new Polygon();
			Point centro = new Point();
			centro.setX(longitude);
			centro.setY(latitude);
			area.setCircle(centro, distance);
			filter.setGeometryByRef(area);

			return _featureClass.search(filter, false);
		} catch (Exception ex) {
			Logger.getLogger(GISLayer.class.getName()).log(Level.SEVERE, null, ex);
		}

		return null;
	}
}
